# -*- coding: utf-8 -*-
import scrapy

import re
import logging
import urllib.parse
from copy import copy, deepcopy
from FDC_spider.constants import BD_RM_STATUS_DICT
from FDC_spider.items import FdcEstateGuidItem, FdcBuildingItem, FdcRoomItem

logger = logging.getLogger(__name__)


class BaodingSpider(scrapy.Spider):
    name = 'baoding'
    allowed_domains = ['bdfdc.net']
    start_urls = ['http://www.bdfdc.net/loadAllProjects.jspx?pageIndex=1']
    pt_page_temp_url = 'http://www.bdfdc.net/loadAllProjects.jspx?pageIndex={}'  # 项目列表翻页url

    def parse(self, response):
        page_str = response.xpath("//div[@class='tiaozhuanye']/text()").extract_first()
        page_li = re.findall(r'\d+', page_str)
        current_page = page_li[1]
        total_page = page_li[0]
        # 获取当前页项目分组
        pt_li = response.xpath("//div[@class='lie_right_center']/ul/li")
        if len(pt_li):
            for pt in pt_li:
                item_eg = FdcEstateGuidItem()
                item_eg['projectName'] = pt.xpath(".//p[1]/a/text()").extract_first()
                item_eg['projectUrl'] = pt.xpath(".//p[1]/a/@href").extract_first()
                item_eg['projectUrl'] = urllib.parse.urljoin(response.request.url, item_eg['projectUrl'])
                projectId_li = re.findall(r'projectId=(.*)', item_eg['projectUrl']) if item_eg['projectUrl'] else []
                item_eg['projectId'] = projectId_li[0] if len(projectId_li) else None
                p2_str = pt.xpath(".//p[2]/text()").extract_first()
                salesTel_li = re.findall(r'\d.*\d', p2_str) if p2_str else []
                item_eg['salesTel'] = salesTel_li[0] if len(salesTel_li) else None
                developerName_li = re.findall(r'开发商:(.*)', p2_str) if p2_str else []
                item_eg['developerName'] = developerName_li[0] if len(developerName_li) else None
                p3_str = pt.xpath(".//p[3]/text()").extract_first()
                estateAddress_li = re.findall(r'楼盘地址:(.*)', ''.join([i for i in p3_str if i.strip()])) if p3_str else []
                item_eg['estateAddress'] = estateAddress_li[0] if len(estateAddress_li) else None
                yield item_eg
                yield response.follow(
                    item_eg['projectUrl'],
                    callback=self.parse_bd_li,
                    meta=dict(item_eg=deepcopy(item_eg)),
                )
        else:
            logger.warning('{}  第{}/{}页 项目列表获取为空'.format(response.request.url, current_page, total_page))

        # 翻页
        print('>>>>>>>>第{}/{}页<<<<<<<<'.format(current_page, total_page))
        if int(current_page) < int(total_page):
            yield scrapy.Request(
                BaodingSpider.pt_page_temp_url.format(str(int(current_page) + 1)),
            )

    def parse_bd_li(self, response):
        """
        获取楼栋列表
        :param response:
        :return:
        """
        item_eg = copy(response.meta['item_eg'])
        # 获取page参数
        page_str = response.xpath("//div[@class='tiaozhuanye']/text()").extract_first()
        page_li = re.findall(r'\d+', page_str)
        current_page = page_li[1]
        total_page = page_li[0]
        # 获取楼栋分组
        a_li = response.xpath("//div[@class='lie_right_center']/div/a")
        if len(a_li):
            for a in a_li:
                item_bd = FdcBuildingItem()
                item_bd['projectName'] = item_eg['projectName']
                item_bd['projectId'] = item_eg['projectId']
                item_bd['developerName'] = item_eg['developerName']
                item_bd['blockName'] = a.xpath("./text()").extract_first()
                item_bd['buildingUrl'] = a.xpath("./@href").extract_first()
                item_bd['buildingUrl'] = urllib.parse.urljoin(response.request.url, item_bd['buildingUrl'])
                yield item_bd
                yield response.follow(
                    item_bd['buildingUrl'],
                    callback=self.parse_rm_li,
                    meta=dict(item_bd=deepcopy(item_bd)),
                )
        else:
            logger.warning(
                '{}  {}-{} 楼栋列表获取为空'.format(response.request.url, item_eg['projectName'], item_eg['developerName']))

        # 翻页
        next_page_temp_url = 'http://www.bdfdc.net/loadBuildInfo.jspx?pageIndex={}&projectId={}'
        if int(current_page) < int(total_page):
            yield scrapy.Request(
                next_page_temp_url.format(str(int(current_page) + 1), item_eg['projectId']),
                callback=self.parse_bd_li,
                meta=dict(item_eg=deepcopy(item_eg)),
            )

    def parse_rm_li(self, response):
        """
        获取房号列表
        :param response:
        :return:
        """
        item_bd = copy(response.meta['item_bd'])
        # 获取单元分组
        unit_li = response.xpath("//td[text()='层数']/following-sibling::td")
        if len(unit_li):
            for i, unit in enumerate(unit_li):
                item_rm = FdcRoomItem()
                item_rm['projectName'] = item_bd['projectName']
                item_rm['projectId'] = item_bd['projectId']
                item_rm['blockName'] = item_bd['blockName']
                item_rm['unitName'] = unit.xpath("./text()").extract_first()
                # 获取当前单元的楼层分组
                floor_li = response.xpath("//td[text()='层数']/../following-sibling::tr")
                if len(floor_li):
                    for floor in floor_li:
                        item_rm['roomFloor'] = floor.xpath("./td[1]/text()").extract_first()
                        # 获取当前楼层的房号分组
                        rm_li = floor.xpath("./td[2]/table//td")
                        if len(rm_li):
                            for rm in rm_li:
                                item_rm['roomNo'] = rm.xpath(".//text()").extract_first()
                                a_str = rm.xpath(".//a/@wf").extract_first()
                                roomUse_li = re.findall(r'用途:(.*)<', a_str) if a_str else []
                                item_rm['roomUse'] = roomUse_li[0] if len(roomUse_li) else None
                                roomArea_li = re.findall(r'建筑面积:(.*) m<', a_str) if a_str else []
                                item_rm['roomArea'] = roomArea_li[0] if len(roomArea_li) else None
                                rm_bgcolor = rm.xpath("./@bgcolor").extract_first()
                                item_rm['saleStatus'] = BD_RM_STATUS_DICT.get(rm_bgcolor, '其它')
                                yield item_rm
                else:
                    logger.warning(
                        '{}  {}-{}-{}-{} 楼层列表获取为空'.format(response.request.url, item_bd['projectName'],
                                                          item_bd['projectId'], item_bd['blockName'],
                                                          item_bd['unitName']))
        else:
            logger.warning(
                '{}  {}-{}-{} 单元列表获取为空'.format(response.request.url, item_bd['projectName'], item_bd['projectId'],
                                               item_bd['blockName']))
